home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / DATABASE / OBJ1_2.ZIP;1 / C_CHOICE.PRG < prev    next >
Encoding:
Text File  |  1993-01-21  |  5.6 KB  |  179 lines

  1. //*****************************************************************************
  2. // C_Choice.prg
  3. // AChoice menu class for OBJECT v2.03
  4. // Copyright (c) 1991, JHK, JHK-Software, Piestany
  5. // Compile with: /N/M/W/A
  6. //-----------------------------------------------------------------------------
  7.  
  8. #include "InKey.ch"
  9. #include "AChoice.ch"
  10. #include "Object.ch"
  11.  
  12. static CurChoice:=nil   //currently active choice object
  13.  
  14. create class Choice from Win
  15.   export:
  16.   var Items      // {}                  //{"item1",...}
  17.   var SelItems   // {}                  //{lSelectable,...}
  18.   var Cursor     // (object of Cursor)
  19.   var Choice     // 1                   //user selected
  20.   var Top        // 0                   //top item in menu
  21.   var CanAppend  // false               //append/delete command allowed
  22.   var InsBlock   // {|o|nil}            //array insert_row_code_block
  23.   var DelBlock   // {|o|nil}            //array delete_row_code_block
  24.   var InRow      // 0                   //input values, need for reinit
  25.   var InCol      // 0                   // after Ins/Del command
  26.   var InCurSize  // 0                    // ...
  27.   method New=ChoiceNew                   //o:New()
  28.   method Init=ChoiceInit                 //o:Init(Name,R,C,CurSize,Items,SelItems,Clr,Shadow)
  29.   method FastInit=ChoiceFastInit         //o:FastInit(Name,R,C,CurSize,Items,SelItems,Clr,Shadow)
  30.   method Process=ChoiceProcess           //o:Process()
  31.   method Done=ChoiceDone                 //o:Done()
  32.   endclass
  33.  
  34.  
  35. //*****************************************************************************
  36. // Choice:New() --> self
  37. // initialize new object
  38. //
  39. constructor ChoiceNew()
  40.   ::Items:= {}
  41.   ::SelItems:= {}
  42.   ::Cursor:= (object of Cursor)
  43.   ::Choice:= 1
  44.   ::Top:= 0
  45.   ::CanAppend:= false
  46.   ::InsBlock:= {|o|nil}
  47.   ::DelBlock:= {|o|nil}
  48.   ::InRow:= 0
  49.   ::InCol:= 0
  50.   ::InCurSize:= 0
  51.   return(self)
  52.  
  53.  
  54. //*****************************************************************************
  55. // Choice:Init(Name,R,C,CurSize,Items,SelItems,Clr,Shadow) --> true
  56. // initialize new simple menu and draw it.
  57. //
  58. method function ChoiceInit(Name,R,C,CurSize,Items,SelItems,Clr,Shadow)
  59.   local Color
  60.   ::FastInit(Name,R,C,CurSize,Items,SelItems,Clr,Shadow)
  61.   ::Paint()
  62.   Color:=SetColor(::Color)
  63.   StuffKey(K_ENTER)
  64.   AChoice(::Row+1,::Col+2,::Row+::RowSize,::Col+::ColSize-1,Items,SelItems,,::Choice,::Top)
  65.   ShowTime()
  66.   SetColor(Color)
  67.   return(true)
  68.  
  69.  
  70. //*****************************************************************************
  71. // Choice:FastInit(Name,R,C,CurSize,Items,SelItems,Clr,Shadow) --> true
  72. // initialize new simple menu, without drawing.
  73. //
  74. method function ChoiceFastInit(Name,R,C,CurSize,Items,SelItems,Clr,Shadow)
  75.   local Rs,Cs
  76.   ::InRow:=R
  77.   ::InCol:=C
  78.   ::InCurSize:=CurSize
  79.   Rs:=Min(Len(Items),MaxRow()-7)
  80.   Cs:=Min(aWidth(Items)+2,MaxCol()-9)
  81.   if Empty(SelItems)
  82.     SelItems:=Array(Len(Items))
  83.     AFill(SelItems,true)
  84.   endif
  85.   ::Cursor:Get()
  86.   ::super(Win):GoodInit(Name,R,C,Rs,Cs,CurSize,Clr,Shadow)
  87.   ::Items:=Items
  88.   ::SelItems:=SelItems
  89.   return(true)
  90.  
  91.  
  92. //*****************************************************************************
  93. // Choice:Process() --> nChoice
  94. // allow user select one from menu items.
  95. //
  96. method function ChoiceProcess()
  97.   local Key
  98.   local Home,End
  99.   local OldChoice:=CurChoice
  100.   local Color:=SetColor(::Color)
  101.   CurChoice:=self
  102.   SaveDOut(ResTxt(140)+if(::CanAppend,","+ResTxt(143),""))
  103.   Home:=SetKey(K_HOME,{||StuffKey(K_CTRL_PGUP)})
  104.   End:=SetKey(K_END,{||StuffKey(K_CTRL_PGDN)})
  105.   SaveHelpIdx({16})
  106.   repeat
  107.     if !::Visible; ::Paint(); endif
  108.     DisableHelp()
  109.     SetKey(nWaitForKey,{||WaitKey()})
  110.     StuffKey(nWaitForKey)
  111.     AChoice(::Row+1,::Col+2,::Row+::RowSize,::Col+::ColSize-1,::Items,::SelItems,"AChoiceFnc",::Choice,::Top)
  112.     SetKey(nWaitForKey,)
  113.     EnableHelp()
  114.     if ::Choice==0
  115.       Key:=LastKey()
  116.       if Key==K_INS; Eval(::InsBlock,self); endif
  117.       if Key==K_DEL; Eval(::DelBlock,self); endif
  118.       if Key==K_INS or Key==K_DEL
  119.         ::Hide()
  120.         ::FastInit(::Name,::InRow,::InCol,::InCurSize,::Items,::SelItems,::Color,::Shadow)
  121.       endif
  122.     endif
  123.   until ::Choice<>0
  124.   RestHelpIdx()
  125.   SetKey(K_HOME,Home)
  126.   SetKey(K_END,End)
  127.   RestDOut()
  128.   CurChoice:=OldChoice
  129.   SetColor(Color)
  130.   return(::Choice)
  131.  
  132.  
  133. static procedure WaitKey()
  134.   @ CurChoice:Row+CurChoice:Top+1,CurChoice:Col+2 say CurChoice:Items[CurChoice:Choice] color ListAsArray(SetColor())[nEnhanced]
  135.   while NextKey()==0; ShowTime(); endwhile
  136.   @ CurChoice:Row+CurChoice:Top+1,CurChoice:Col+2 say CurChoice:Items[CurChoice:Choice]
  137.   return
  138.  
  139.  
  140. function AChoiceFnc(Mode,Idx,Top)
  141.   local nKey:=LastKey()
  142.   local r:=AC_CONT
  143.   CurChoice:Top:=Top
  144.   CurChoice:Choice:=Idx
  145.   if Mode==AC_HITTOP and Set(_SET_WRAP)
  146.     StuffKey(K_CTRL_PGDN)
  147.   elseif Mode==AC_HITBOTTOM and Set(_SET_WRAP)
  148.     StuffKey(K_CTRL_PGUP)
  149.   elseif Mode==AC_EXCEPT
  150.     if Upper(Chr(nKey)) $ "ABCDEFGHIJKLMNOPQRSTUYVWXYZ0123456789"
  151.       StuffKeys(" ")
  152.       retur(AC_GOTO)
  153.     elseif nKey==K_ENTER or nKey==K_CTRL_RET
  154.       CurChoice:Choice:=Idx
  155.       r:=AC_SELECT
  156.     elseif nKey==K_ESC or nKey==nSwapTask
  157.       CurChoice:Choice:=-Idx
  158.       r:=AC_ABORT
  159.     elseif (nKey==K_INS or nKey==K_DEL) and CurChoice:CanAppend
  160.       CurChoice:Choice:=0
  161.       r:=AC_SELECT
  162.     endif
  163.   endif
  164.   if NextKey()==0; StuffKey(nWaitForKey); endif
  165.   return(r)
  166.  
  167.  
  168. //*****************************************************************************
  169. // Choice:Done() --> true
  170. // destroy the simple menu.
  171. //
  172. method function ChoiceDone()
  173.   ::Hide()
  174.   ::Cursor:Set()
  175.   return(true)
  176.  
  177. //------------------------------------------------------- eof (c)JHK ----------
  178.  
  179.